package com.locationbasedagent;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class HouseListingActivity extends Activity {
    /** Called when the activity is first created. */
  
	private ImageAdapter mImageAdapter;
	private ImageView mSelectedImage;
	private Button mViewPhotosButton;
	private Button mViewVideoButton;
	private Button mSendSMSButton;
	private Button mCallButton;
	private TextView mHouseName;
	private TextView mHouseAddress;
	private TextView mCityAndZip;
	private TextView mAmount;
	
	private LocalHouseListing houseListing;
	private AsyncFetchTask task;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Bundle bundle = getIntent().getExtras();
        
        houseListing = (LocalHouseListing) bundle.getParcelable("listing");
		
		mHouseName = (TextView) findViewById(R.id.housename);
		mHouseName.setText(houseListing.getListingName());
		mHouseAddress = (TextView) findViewById(R.id.address);
		mHouseAddress.setText(houseListing.getListingAddress());
		mCityAndZip = (TextView) findViewById(R.id.cityandzip);
		mCityAndZip.setText(houseListing.getListingCity() + " " + houseListing.getListingZip());
		mAmount = (TextView) findViewById(R.id.price);
		mAmount.setText(houseListing.getListingPrice());

	    mSelectedImage = (ImageView) findViewById(R.id.imageView1);
	    mImageAdapter = new ImageAdapter(this);
	    String[] photoURL = { houseListing.getPhotoURL1()};
	    mImageAdapter.setImageURLs(photoURL);
		mImageAdapter.getImageDownloader().download(mImageAdapter.getItem(0), mSelectedImage);
		
        mViewPhotosButton = (Button) findViewById(R.id.button1);
        mViewPhotosButton.setOnClickListener(mViewPhotosListener);
        mViewVideoButton = (Button) findViewById(R.id.button2);
        mViewVideoButton.setOnClickListener(mViewVideoListener);
        
        mSendSMSButton = (Button) findViewById(R.id.button3);
        mSendSMSButton.setOnClickListener(mSendSMSListener);
        
        mCallButton = (Button) findViewById(R.id.button4);
        mCallButton.setOnClickListener(mCallPhoneListener);

    }
    
    private OnClickListener mViewPhotosListener = new OnClickListener() {
        public void onClick(View v) {        	
            Intent intent =
                    new Intent(HouseListingActivity.this,
                            PhotoViewerActivity.class);
            Bundle bundle = new Bundle();
            
    		bundle.putString("photo1", houseListing.getPhotoURL1());
    		bundle.putString("photo2", houseListing.getPhotoURL2());
    		bundle.putString("photo3", houseListing.getPhotoURL3());
    		bundle.putString("photo4", houseListing.getPhotoURL4());
    		bundle.putString("photo5", houseListing.getPhotoURL5());
    		
    		intent.putExtras(bundle);
    		startActivity(intent);

        }
    };
    
    private OnClickListener mViewVideoListener = new OnClickListener() {
        public void onClick(View v) {        	
            Intent intent =
                    new Intent(HouseListingActivity.this,
                            VideoPlayerActivity.class);
            Bundle bundle = new Bundle();
            
    		bundle.putString("videoInfo", houseListing.getVideoURL());
    		intent.putExtras(bundle);
            startActivity(intent);
            

        }
    };
    
    private OnClickListener mSendSMSListener = new OnClickListener() {
        public void onClick(View v) {        	
            Intent intent =
                    new Intent(HouseListingActivity.this,
                            SMSComposerActivity.class);
            
            Bundle bundle = new Bundle();
            
    		bundle.putString("contactinfo", houseListing.getSellingAgentPhoneNumber());
    		intent.putExtras(bundle);
            startActivity(intent);
            

        }
    };
    
    private OnClickListener mCallPhoneListener = new OnClickListener() {        	
    	public void onClick(View v) {
	    	try {
	            Intent callIntent = new Intent(Intent.ACTION_CALL);
	            callIntent.setData(Uri.parse("tel:" + houseListing.getSellingAgentPhoneNumber()));
	            startActivity(callIntent);
	        } catch (ActivityNotFoundException e) {
	            Log.e("lab2", "Call failed", e);
	        }

    	}
    };
    

    
}